package com.flt.module.homepage.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.flt.module.base.controller.BaseController;
import com.flt.module.homepage.service.IHomepageService;

@Controller
@RequestMapping("/homepage/*")
public class HomepageController extends BaseController {
	
	@Autowired
	private IHomepageService service;

	@RequestMapping("page")
	public String page(HttpServletRequest request ,Model model){
		
		
		model.addAttribute("menus", service.findAllMenus());
		model.addAttribute("defaultMenu", service.findDefaultMenu());
		model.addAttribute("basePath", super.getBasePath(request));
		return "homepage/page.ftl";
	}
	
	/**
	 * 站长推荐板块
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("recommend")
	public String recommend(HttpServletRequest request,Model model){
		
		model.addAttribute("basePath", super.getBasePath(request));
		return "homepage/recommend.ftl";
	}
	
	
	/**
	 * 登陆板块
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("login")
	public String login(HttpServletRequest request,Model model){
		
		return "homepage/login.ftl";
	}
	
	/**
	 * 
	 * @param menuId
	 * @param sortby 1-最新上架 ,2-销量优先,3-会员好评
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("getproducts/{menuid}")
	public String getProduct(@PathVariable("menuid") Integer menuId,Integer sortby,HttpServletRequest request,Model model){
		
		if(sortby==null)sortby=1;
		
		model.addAttribute("sortby", sortby);
		model.addAttribute("currentMenu", service.findMenuById(menuId));
		model.addAttribute("menus", service.findAllMenus());
		model.addAttribute("products", service.findProductsByMenuId(menuId,sortby));
		model.addAttribute("basePath", super.getBasePath(request));
		return "homepage/product-grid.ftl";
	}
	
}
